package com.triones.api.controller.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.triones.api.entity.SandBoxTransactionRequestBodyBO;
import com.triones.api.entity.bo.*;
import com.triones.api.entity.param.BankCardTxnParams;
import com.triones.api.entity.param.CardBinParams;
import com.triones.api.entity.vo.*;
import com.triones.api.service.BankCardService;
import com.triones.api.service.KycVerifyService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * api - 银行卡管理
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/bankCard")
public class ApiBankCardController extends BaseController {

    private final KycVerifyService kycVerifyService;

    private final BankCardService bankCardService;

    /**
     * 检查是否需要KYC认证
     * true:需要、false:不需要
     * @return
     */
    @GetMapping("/checkKyc")
    public R<Boolean> checkIsKyc() {
        return R.ok("操作成功！",kycVerifyService.checkIsKyc());
    }

    /**
     * 获取最近一次kyc验证信息
     * @return
     */
    @GetMapping("/kycInfo")
    public R<KycVerifyVO> kycInfo() {
        return R.ok(kycVerifyService.apiKycInfo());
    }

    /**
     * kyc验证
     * @param bo
     * @return
     */
    @PostMapping("/verify")
    public R<Void> kycVerify(@Validated @RequestBody KycVerifyBO bo) {
        kycVerifyService.apiKycVerify(bo);
        return R.ok();
    }

    /**
     * 银行卡列表（分页）
     * @return
     */
    @GetMapping("/pageList")
    public TableDataInfo<BankCardVO> pageList(PageQuery pageQuery) {
        return bankCardService.apiPageList(pageQuery);
    }

    /**
     * 银行卡申请记录列表（分页）
     * @return
     */
    @GetMapping("/apply/pageList")
    public TableDataInfo<BankCardVO> applyPageList(PageQuery pageQuery) {
        return bankCardService.apiApplyPageList(pageQuery);
    }

    /**
     * 银行卡详情
     * @return
     */
    @GetMapping("/detail/{id}")
    public R<CustomBankCardVO> detail(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(bankCardService.apiDetail(id));
    }

    /**
     * 银行卡申请
     * @param bo
     * @return
     */
    @PostMapping("/apply")
    public R<Void> cardApply(@Validated @RequestBody BankCardBO bo) {
        return toAjax(bankCardService.cardApply(bo));
    }

    /**
     * 检查卡bin信息
     */
    @GetMapping("/check/cardBin")
    public R<Void> checkCardBin(CardBinParams params) {
        return toAjax(bankCardService.checkCardBin(params));
    }

    /**
     * 卡片激活
     * @param bo
     * @return
     */
    @PostMapping("/activate")
    public R<Void> cardActivate(@RequestBody CardActivateBO bo) {
        return toAjax(bankCardService.cardActivate(bo));
    }

    /**
     * 卡片冻结
     * @param bo
     * @return
     */
    @PostMapping("/freeze")
    public R<Void> freezeCard(@RequestBody BankCardFreezeRecordBO bo) {
        return toAjax(bankCardService.freezeCard(bo));
    }

    /**
     * 银行卡交易记录/提现记录（分页）
     */
    @GetMapping(value = "/txn/list")
    public TableDataInfo<BankCardTxnVO> cardTnx(@Validated BankCardTxnParams params, PageQuery pageQuery) {
        return bankCardService.cardTnx(params,pageQuery);
    }

    /**
     * 银行卡交易详情
     */
    @GetMapping(value = "/txn/detail/{id}")
    public R<BankCardTxnVO> cardTnxDetail(@NotNull(message = "交易不能为空") @PathVariable Long id) {
        return R.ok(bankCardService.cardTnxDetail(id));
    }

    /**
     * 银行卡列表
     * @return
     */
    @GetMapping("/list")
    public R<List<BankCardVO>> listAll() {
        return R.ok(bankCardService.listAll());
    }

    /**
         * 银行卡首页交易记录（分页）
     */
    @GetMapping(value = "/homePage/txn/list")
    public TableDataInfo<BankCardTxnVO> homePageCardTnx(BankCardTxnParams params, PageQuery pageQuery) {
        return bankCardService.cardTnx(params, pageQuery);
    }

    /**
     * 卡种列表
     */
    @GetMapping("/cardType/list")
    public R<List<BankCardTypeVO>> cardTypeList() {
        return R.ok(bankCardService.cardTypeList());
    }

    @SaIgnore
    @GetMapping("/text/txn")
    public R<Map> textTxn(@Validated SandBoxTransactionRequestBodyBO bo) {
        return R.ok(bankCardService.textTxn(bo));
    }
    @SaIgnore
    @GetMapping("/mock/cardDetail")
    public R<Map> mockCardDetail(String cardId) {
        return R.ok(bankCardService.getBankCardDetail(cardId));
    }

    @SaIgnore
    @GetMapping("/mock/cardUpdate")
    public R<Map> mockCardUpdate(UpdateCardBO bo) {
        return R.ok(bankCardService.mockCardUpdate(bo));
    }
}
